﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Echart.BLL.Echarts
{
    public class HtmlManager
    {
        private string sourceFile = "Echart.Foreground.Echarts\\echarts\\SourceFile.html";
        private string destFile = "Echart.Foreground.Echarts\\echarts";
        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="sourceFolder">原文件路径</param>
        /// <param name="destFolder">目标文件路径</param>
        /// <returns></returns>
        private string CopyFile(string sourceFile, string destFolder, string destFileName)
        {
            try
            {
                //如果目标路径不存在,则创建目标路径
                if (!System.IO.Directory.Exists(destFolder))
                {
                    System.IO.Directory.CreateDirectory(destFolder);
                }
                string dest = System.IO.Path.Combine(destFolder, destFileName);
                System.IO.File.Copy(sourceFile, dest);//复制文件
                return dest;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return "";
            }

        }

        /// <summary>
        /// 获取原始文件路径
        /// </summary>
        /// <returns></returns>
        private string GetSourceFile()
        {           
            DirectoryInfo info = new DirectoryInfo(Application.StartupPath);

            String path = info.Parent.Parent.Parent.FullName;
            path += "\\";
            path += sourceFile;

            return path;
        }

        /// <summary>
        /// 获取目标文件夹
        /// </summary>
        /// <returns></returns>
        private string GetDestFolder()
        {
            DirectoryInfo info = new DirectoryInfo(Application.StartupPath);

            String path = info.Parent.Parent.Parent.FullName;
            path += "\\";
            path += destFile;

            return path;
        }

        /// <summary>
        /// 生成目标html文件
        /// </summary>
        /// <param name="destFile">目标文件名</param>
        /// <returns></returns>
        public string CreateHtml(string destFile)
        {
            string sourceFolder = GetSourceFile();
            string destFolder = GetDestFolder();
            return CopyFile(sourceFolder, destFolder, destFile);
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="destFile">目标文件名</param>
        /// <returns></returns>
        public bool IsHtmlExist(string destFile)
        {
            string destFolder = GetDestFolder();
            var files = Directory.GetFiles(destFolder, "*.html");
            foreach(var item in files)
            {
                string file = item.Substring(item.LastIndexOf("\\") + 1);
                if (file == destFile)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
